package Controller.BDD;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Model.Droit;

public class GestionRequeteDroit extends GestionnaireRequeteBDD {

	public GestionRequeteDroit(){

	}
	

	public ArrayList<Droit> recupererListeDonnees(String requete){
		ArrayList<Droit> liste = new ArrayList<Droit>();
		Connection connection = this.getConnexion();
		if(connection != null){
			Statement statement;
			try {
				statement = connection.createStatement();
				ResultSet res = statement.executeQuery(requete);
				while(res.next()){
					int id = res.getInt(1);
					String libelle = res.getString(2);
					String description = res.getString(3);
					String page = res.getString(4);
					liste.add(new Droit(id,libelle,description, page));
				}
			}catch (SQLException e){
				e.printStackTrace();
			}
			if(liste.size()==0){
				this.resetConnexion(connection);
				return null;
			}
			else{
				this.resetConnexion(connection);
				return liste;
			}
		}
		else{
			return null;
		}
	}
	
	public Droit recupererDonnee(String requete){
		Connection connection = this.getConnexion();
		Droit droit = null;
		if(connection != null){
			Statement statement;
			try {
				statement = connection.createStatement();
				ResultSet res = statement.executeQuery(requete);
				res.next();
				int id = res.getInt(1);
				String libelle = res.getString(2);
				String description = res.getString(3);
				String page = res.getString(4);
				droit = new Droit(id,libelle,description, page);
			}catch (SQLException e){
				e.printStackTrace();
			}
			this.resetConnexion(connection);
		}
		return droit;
	}

	public boolean executeStatement(String requete) {
		Connection connection = this.getConnexion();
		boolean res = false;
		if(connection != null){
			Statement statement;
			try {
				statement = connection.createStatement();
				statement.execute(requete);
				if(statement.execute(requete)){
					res=true;
				}
			}catch (SQLException e){
				e.printStackTrace();
			}
			this.resetConnexion(connection);
		}
		return res;
	}
}
